
##############################################
# IRT Discrimination Parameters Lollipop Chart
##############################################

# Extract discrimination parameters for EA interested positive ID
df_EA_a_yes_pos<-coef(mEA_yes_pos, IRTpars = TRUE, simplify = TRUE) # extract coefficients from model
df_EA_a_yes_pos<-data.frame(df_EA_a_yes_pos) # make data frame
para_EA_yes_pos<-df_EA_a_yes_pos$items.a # extract discrimination parameters
para_EA_yes_pos<-data.frame(para_EA_yes_pos) # make data frame
para_EA_yes_pos<-para_EA_yes_pos%>% # add names of items in a colomn
  mutate(items=c("int_pos_id_YES_1", "int_pos_id_YES_2", "int_pos_id_YES_3", "int_pos_id_YES_4", "int_pos_id_YES_5", "int_pos_id_YES_6", "int_pos_id_YES_7", "int_pos_id_YES_8"))
# Extract discrimination parameters for CA interested positive ID
df_CA_a_yes_pos<-coef(mCA_yes_pos, IRTpars = TRUE, simplify = TRUE) 
df_CA_a_yes_pos<-data.frame(df_CA_a_yes_pos)
para_CA_yes_pos<-df_CA_a_yes_pos$items.a
para_CA_yes_pos<-data.frame(para_CA_yes_pos)
para_CA_yes_pos<-para_CA_yes_pos%>% 
  mutate(items=c("int_pos_id_YES_1", "int_pos_id_YES_2", "int_pos_id_YES_3", "int_pos_id_YES_4", "int_pos_id_YES_5", "int_pos_id_YES_6", "int_pos_id_YES_7", "int_pos_id_YES_8"))


# Extract discrimination parameters for EA interested negative ID
df_EA_a_yes_neg<-coef(mEA_yes_neg, IRTpars = TRUE, simplify = TRUE) 
df_EA_a_yes_neg<-data.frame(df_EA_a_yes_neg) 
para_EA_yes_neg<-df_EA_a_yes_neg$items.a
para_EA_yes_neg<-data.frame(para_EA_yes_neg)
para_EA_yes_neg<-para_EA_yes_neg%>% 
  mutate(items=c("int_neg_id_YES_1", "int_neg_id_YES_2", "int_neg_id_YES_3", "int_neg_id_YES_4", "int_neg_id_YES_5", "int_neg_id_YES_6", "int_neg_id_YES_7", "int_neg_id_YES_8"))
# Extract discrimination parameters for CA interested negative ID
df_CA_a_yes_neg<-coef(mCA_yes_neg, IRTpars = TRUE, simplify = TRUE) 
df_CA_a_yes_neg<-data.frame(df_CA_a_yes_neg) 
para_CA_yes_neg<-df_CA_a_yes_neg$items.a 
para_CA_yes_neg<-data.frame(para_CA_yes_neg)
para_CA_yes_neg<-para_CA_yes_neg%>% 
  mutate(items=c("int_neg_id_YES_1", "int_neg_id_YES_2", "int_neg_id_YES_3", "int_neg_id_YES_4", "int_neg_id_YES_5", "int_neg_id_YES_6", "int_neg_id_YES_7", "int_neg_id_YES_8"))

# Extract discrimination parameters for EA uninterested positive
df_EA_a_no_pos<-coef(mEA_no_pos, IRTpars = TRUE, simplify = TRUE) 
df_EA_a_no_pos<-data.frame(df_EA_a_no_pos) 
para_EA_no_pos<-df_EA_a_no_pos$items.a 
para_EA_no_pos<-data.frame(para_EA_no_pos)
para_EA_no_pos<-para_EA_no_pos%>% 
  mutate(items=c("int_pos_id_no_1", "int_pos_id_no_2", "int_pos_id_no_3", "int_pos_id_no_4", "int_pos_id_no_5", "int_pos_id_no_6", "int_pos_id_no_7", "int_pos_id_no_8"))
# Extract discrimination parameters for CA uninterested positive
df_CA_a_no_pos<-coef(mCA_no_pos, IRTpars = TRUE, simplify = TRUE) 
df_CA_a_no_pos<-data.frame(df_CA_a_no_pos) 
para_CA_no_pos<-df_CA_a_no_pos$items.a 
para_CA_no_pos<-data.frame(para_CA_no_pos)
para_CA_no_pos<-para_CA_no_pos%>% 
  mutate(items=c("int_pos_id_no_1", "int_pos_id_no_2", "int_pos_id_no_3", "int_pos_id_no_4", "int_pos_id_no_5", "int_pos_id_no_6", "int_pos_id_no_7", "int_pos_id_no_8"))


# Extract discrimination parameters for EA uninterested negative
df_EA_a_no_neg<-coef(mEA_no_neg, IRTpars = TRUE, simplify = TRUE) 
df_EA_a_no_neg<-data.frame(df_EA_a_no_neg) 
para_EA_no_neg<-df_EA_a_no_neg$items.a 
para_EA_no_neg<-data.frame(para_EA_no_neg)
para_EA_no_neg<-para_EA_no_neg%>% 
  mutate(items=c("int_neg_id_no_1", "int_neg_id_no_2", "int_neg_id_no_3", "int_neg_id_no_4", "int_neg_id_no_5", "int_neg_id_no_6", "int_neg_id_no_7", "int_neg_id_no_8"))
# Extract discrimination parameters for CA uninterested negative
df_CA_a_no_neg<-coef(mCA_no_neg, IRTpars = TRUE, simplify = TRUE) 
df_CA_a_no_neg<-data.frame(df_CA_a_no_neg) 
para_CA_no_neg<-df_CA_a_no_neg$items.a 
para_CA_no_neg<-data.frame(para_CA_no_neg)
para_CA_no_neg<-para_CA_no_neg%>% 
  mutate(items=c("int_neg_id_no_1", "int_neg_id_no_2", "int_neg_id_no_3", "int_neg_id_no_4", "int_neg_id_no_5", "int_neg_id_no_6", "int_neg_id_no_7", "int_neg_id_no_8"))


# Merge EA and CA
df_para_yes_pos<-merge(para_EA_yes_pos, para_CA_yes_pos, by= "items") # merge EA and CA by items
df_para_yes_pos <- df_para_yes_pos %>% # rename items
  mutate(item = ifelse(grepl("YES", items), paste0("Item ", gsub("int_pos_id_YES_", "", items))))

df_para_yes_neg<-merge(para_EA_yes_neg,para_CA_yes_neg, by= "items") # merge EA and CA by items
df_para_yes_neg <- df_para_yes_neg %>%
  mutate(item = ifelse(grepl("YES", items), paste0("Item ", gsub("int_neg_id_YES_", "", items))))

# merge EA and CA
df_para_no_pos<-merge(para_EA_no_pos,para_CA_no_pos, by= "items") # merge EA and CA by items
df_para_no_pos <- df_para_no_pos %>%
  mutate(item = ifelse(grepl("no", items), paste0("Item ", gsub("int_pos_id_no_", "", items))))

# merge EA and CA
df_para_no_neg<-merge(para_EA_no_neg,para_CA_no_neg, by= "items") # merge EA and CA by items
df_para_no_neg <- df_para_no_neg %>%
  mutate(item = ifelse(grepl("no", items), paste0("Item ", gsub("int_neg_id_no_", "", items))))

# Merge all positive items
names(df_para_yes_pos) <- c("items", "para_EA", "para_CA", "Item") # rename cols interested pos id
names(df_para_no_pos) <- c("items", "para_EA", "para_CA", "Item") # rename cols uninterested pos id
df_para_pos <- rbind(df_para_yes_pos, df_para_no_pos) # make one data frame for positive ID with EA and CA values
df_para_pos <- df_para_pos %>% # rename items
  mutate(item = ifelse(grepl("YES", items), paste0("I", gsub("int_pos_id_YES_", "", items)), paste0("I", gsub("int_pos_id_no_", "", items), "*")))

# Group by the last number of item name
df_para_pos <- df_para_pos %>%
  mutate(last_number = as.numeric(gsub("\\D", "", items))) %>%
  arrange(last_number)

# Merge all negative items
names(df_para_yes_neg) <- c("items", "para_EA", "para_CA", "Item") # rename cols for interested neg id
names(df_para_no_neg) <- c("items", "para_EA", "para_CA", "Item") # rename cols for uninterested neg id
df_para_neg <- rbind(df_para_yes_neg, df_para_no_neg) # make one data frame for negative ID with EA and CA values
df_para_neg <- df_para_neg %>% # rename items
  mutate(item = ifelse(grepl("YES", items), paste0("I", gsub("int_neg_id_YES_", "", items)), paste0("I", gsub("int_neg_id_no_", "", items), "*")))

# Group by the last number of item name
df_para_neg <- df_para_neg %>%
  mutate(last_number = as.numeric(gsub("\\D", "", items))) %>%
  arrange(last_number)

# Merge everything
df_para <- rbind(df_para_yes_pos, df_para_yes_neg, df_para_no_pos, df_para_no_neg) # make one data frame 

# Create a new column 'group' based on the 'items' column
df_para$Item <- as.character(df_para$Item)  # Convert to character if not already
df_para$Item <- factor(df_para$Item,      # Reordering group factor levels
                       levels = c("Item 8", "Item 7", "Item 6", "Item 5", "Item 4", "Item 3", "Item 2", "Item 1"))


df_para$group <- ifelse(grepl("pos", df_para$items) & grepl("YES", df_para$items), "Panel A: Interested Positive Identity",
                        ifelse(grepl("pos", df_para$items) & grepl("no", df_para$items), "Panel B: Uninterested Positive Identity",
                               ifelse(grepl("neg", df_para$items) & grepl("YES", df_para$items), "Panel C: Interested Negative Identity",
                                      ifelse(grepl("neg", df_para$items) & grepl("no", df_para$items), "Panel D: Uninterested Negative Identity", NA))))


df_para$group <- factor(df_para$group,      # Reordering group factor levels
                        levels = c("Panel A: Interested Positive Identity", "Panel B: Uninterested Positive Identity", "Panel C: Interested Negative Identity", "Panel D: Uninterested Negative Identity"))

# Lollipop plot
df_para$Item<-as.factor(df_para$Item)

lolli<-ggplot(df_para) +
  geom_segment(aes(x = Item, xend = Item, y = para_EA, yend = para_CA), 
               linewidth = 0.4, colour = "black") + # line between dots
  # define the EA and CA dots
  geom_point(aes(x = Item, y = para_EA, color = "Exploratory Analysis"), size = 2.5, shape=1) +
  geom_point(aes(x = Item, y = para_CA, color = "Confirmatory Analysis"), size = 2.5, shape=19) + 
  # set colors and disable legend for colors
  scale_color_manual(values = c("Exploratory Analysis" = "black", "Confirmatory Analysis" = "black"), guide = FALSE) +
  # add dotted line with threshold in PAP
  geom_hline(yintercept = c(0.5, 4), linetype = "dotted", color = "#B0171F", linewidth=0.8) +
  # make horizontal
  coord_flip() +
  # make 4 separate
  facet_wrap(.~ group, nrow=2)+
  # define the theme
  theme_classic() + 
  # name the labs
  xlab("")+
  ylab("Discrimination Parameter") +
  # specify axis limits and breaks
  scale_y_continuous(limits = c(0, 6), breaks = seq(0, 6, 0.5))+
  # add legend with custom title
  guides(color = guide_legend(title = "", override.aes = list(shape = c(19,1)))) +
  # Move legend inside plot
  theme(legend.position = "bottom")

print(lolli)
